package initiate

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"soul/appconfig"
	"soul/model"
)

var DB *gorm.DB

func MysqlInt() {
	var err error
	data := appconfig.ViperData.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.User, data.Password, data.Host, data.Port, data.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		return
	}
	fmt.Println("mysql连接成功")
	DB.Debug().AutoMigrate(&model.User{}, &model.Release{}, &model.Chat{}, &model.Follow{}, &model.Like{}, &model.Comment{}, &model.Reply{}, &model.Room{}, &model.RoomUser{})
}
